#include <stdio.h>
#include <string.h>

#include "../../../../LIB/xml/libxml/tree.h"
#include "../../../../LIB/xml/libxml/parser.h"



//out
#include "webapixml.h"



const char sz_space[] = {" "};


int	xml_parser_login_RetUid(void *mem,unsigned int nSize, int *lpOutnRetval, unsigned int *lpOutnUserID)
{	
	
	xmlDocPtr doc = NULL;

	xmlNodePtr cur = NULL;

	xmlNodePtr node = NULL;

	int		typeval = 0 ;

	unsigned int	onlineId = 0 ;

	xmlChar *lpcur = NULL ;
	
	doc = xmlParseMemory(mem,nSize);

	if (doc == NULL) 
	{
		return 0 ;
	}
	
	cur = xmlDocGetRootElement(doc) ;

	if (cur == NULL ) 
	{
		xmlFreeDoc(doc);
		return 0 ;
	}
	
	if (0 == strcmp((const char*)cur->name," "))
	{	
		xmlFreeDoc(doc);
		return 0;
	}

	for(node = cur->children; node != NULL; node = node->next) 
	{


		//printf("cur node = %s/n",node->name);
	
		if (0 == strcmp((const char*)node->name,"result"))
		{
			lpcur = xmlGetProp(node,(xmlChar*)"type");

			if (lpcur)
			{
				sscanf((const char*)lpcur,"%d",&typeval);
			}

			//get typeval!

			break;
		}

	}

	for(node = node ->children,lpcur=NULL ; node != NULL; node = node->next) 
	{

	
		if (0 == strcmp((const char*)node->name,"onlineId"))

		{
			lpcur = xmlNodeGetContent(node);

			if (lpcur)
			{
				sscanf((const char*)lpcur,"%d",&onlineId);
				//have get id !
			}
				
			break;
		}
	
	}

	*lpOutnRetval = typeval;

	*lpOutnUserID = onlineId;
					
	xmlFreeDoc(doc);

	//xmlCleanupParser();

	return 1;
}








int	xml_parser_get_Result(void* mem,unsigned int nSize, int *lpOutnResult)
{	
	
	xmlDocPtr doc = NULL;

	xmlNodePtr cur = NULL;

	xmlNodePtr node = NULL;

	int		typeval = 0 ;

	xmlChar *lpcur = NULL ;
	
	doc = xmlParseMemory(mem,nSize);

	if (doc == NULL) 
	{
		return 0 ;
	}
	
	cur = xmlDocGetRootElement(doc) ;

	if (cur == NULL ) 
	{
		xmlFreeDoc(doc);
		return 0 ;
	}
	
	if (0 == strcmp(cur->name," "))
	{	
		xmlFreeDoc(doc);
		return 0;
	}

	for(node = cur->children; node != NULL; node = node->next) 
	{

		if (0 == strcmp(node->name,"result"))
		{
			sscanf(xmlGetProp(node,"type"),"%d",&typeval);
							
			break;
		}

	}

	*lpOutnResult = typeval;
					
	xmlFreeDoc(doc);

	//xmlCleanupParser();

	return 1;
}



int	xml_parser_get_Nodeval
(
void* mem,
unsigned int nSize, 
const char* szNodeName,
int *lpOutnResult
)
{	
	
	xmlDocPtr doc = NULL;

	xmlNodePtr cur = NULL;

	xmlNodePtr node = NULL;

	int		typeval = 0 ;

	xmlChar *lpcur = NULL ;
	
	doc = xmlParseMemory(mem,nSize);

	if (doc == NULL) 
	{
		return 0 ;
	}
	
	cur = xmlDocGetRootElement(doc) ;

	if (cur == NULL ) 
	{
		xmlFreeDoc(doc);
		return 0 ;
	}
	
	if (0 == strcmp(cur->name," "))
	{	
		xmlFreeDoc(doc);
		return 0;
	}

	for(node = cur->children; node != NULL; node = node->next) 
	{
		if ( 0 == strcmp(node->name,szNodeName) )
		{
			sscanf(xmlGetProp(node,"type"),"%d",&typeval);
							
			break;
		}

	}

	*lpOutnResult = typeval;
					
	xmlFreeDoc(doc);

	//xmlCleanupParser();

	return 1;
}




int	xml_parser_GetLicDWORD(void *mem,unsigned int nSize, unsigned long *lpretdw_Lic)
{	
	
	xmlDocPtr	doc = NULL;

	xmlNodePtr	cur = NULL;

	xmlNodePtr	node = NULL;

	xmlChar		*lpcur = NULL ;

	char	sztmp[48] = { 0 } ;

	

	unsigned long	dwtmp = 0 ;

	unsigned long	i,j;
	
	doc = xmlParseMemory(mem,nSize);

	if (doc == NULL) 
	{
		return 0 ;
	}
	
	cur = xmlDocGetRootElement(doc) ;

	if (cur == NULL ) 
	{
		xmlFreeDoc(doc);
		return 0 ;
	}
	
	if (0 == strcmp((const char*)cur->name," "))
	{	
		xmlFreeDoc(doc);
		return 0;
	}

	for(node = cur->children; node != NULL; node = node->next) 
	{


		//printf("cur node = %s\n",node->name);
	
		if (0 == strcmp((const char*)node->name,"result"))
		{
			lpcur = xmlGetProp(node,(xmlChar*)"type");

			if (lpcur)
			{
				//sscanf((const char*)lpcur,"%d",&typeval);
			}

			//get typeval!

			break;
		}

	}

	for(node = node ->children,lpcur=NULL ; node != NULL; node = node->next) 
	{

	
		if (0 == strcmp((const char*)node->name,"dword"))

		{
			lpcur = xmlNodeGetContent(node);

			if (lpcur)
			{
				sscanf((const char*)lpcur,"%s",&sztmp);
				//have get id !
			}
				
			break;
		}
	
	}
	
	for (i=0,j=0;i<32;i++)
	{
		switch(sztmp[i])
		{
			case '0':
				
				break;

			case '1':
				
				dwtmp |=  ( 1 << (0x1f-i) ) ;

				break;

			default:

				break;
		}

	}

	*lpretdw_Lic = dwtmp  ;
						
	xmlFreeDoc(doc);

	//xmlCleanupParser();

	return 1;
}